package com.app.controller.authority;

import com.app.controller.BaseController;
import com.app.entity.Menu;
import com.app.entity.Pagination;
import com.app.entity.RoleList;
import com.app.entity.authority.Power;
import com.app.entity.authority.Role;
import com.app.entity.authority.RoleMenuRelation;
import com.app.service.authority.RoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lijun on 2018/2/22.
 */
@RestController
@RequestMapping("/api/role")
public class RoleController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private RoleService roleService;

    /**
     * 查    role-info列表
     *
     * @param page
     * @return
     */
    @RequestMapping(value = "/getInfoAll", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryRole(@RequestBody Pagination page) {

        Map<String, Object> result = new HashMap<String, Object>();
        try {
            Pagination p = this.roleService.queryRoleAll(page);

            result.put("code", 200);
            result.put("message", "succ");
            result.put("data", p);
            return result;

        } catch (Exception e) {
            log.error("func:queryRole %n page:" + page + "%n" + e.getMessage());
            result.put("code", 500);
            result.put("message", e);
            return result;
        }
    }

    /**
     * 查    modifyrole
     *
     * @param _role
     * @return
     */
    @RequestMapping(value = "/getModifyInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getModifyInfo(@RequestBody Role _role) {

        Map<String, Object> result = new HashMap<String, Object>();
        try {
            List<Role> p = this.roleService.getModifyInfo(_role);
            result.put("code", 200);
            result.put("message", "succ");
            result.put("data", p);
            return result;

        } catch (Exception e) {
            log.error("func:getModifyInfo %n _role:" + _role + "%n" + e.getMessage());
            result.put("code", 500);
            result.put("message", e);
            return result;
        }
    }

    /**
     * 增(角色&权限)
     * *原写:caojianhua  修改:liuboqing  3/12
     *
     * @param _role
     * @return
     */
    @RequestMapping(value = "/addRoleInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> insertRole(@RequestBody Role _role) {

        Map<String, Object> results = new HashMap<String, Object>();

        try {

            Integer resultcount_R = this.roleService.insertRoleAll(_role);

            if (resultcount_R > 0) {
                results.put("code", 200);
                results.put("message", "操作成功！");
                results.put("data", "");

            } else {
                results.put("code", 201);
                results.put("message", "角色编码重复，请选择其他角色编号！");
                results.put("data", "");
            }
            return results;
        } catch (Exception e) {
            log.error("func:insertRole %n _role:" + _role + "%n" + e.getMessage());
            results.put("code", 500);
            results.put("message", e);
            return results;
        }
    }

    /**
     * 判断CODE是否已存在
     *
     * @param _role
     * @return
     */
    @RequestMapping(value = "/validateCode", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> validateCode(@RequestBody Role _role) {

        //ID赋值
        if (_role.getId() == null || _role.getId().length() <= 0) {
            _role.setId(java.util.UUID.randomUUID().toString());
        }

        Map<String, Object> result = new HashMap<String, Object>();
        try {
            List<Role> list = this.roleService.validateLevelCode(_role);

            if (list.size() > 0) {
                result.put("code", 201);
                result.put("message", "编码存在！请选择其他编码");
            } else {
                result.put("code", 200);
                result.put("message", "succ");
            }
            return result;

        } catch (Exception e) {
            log.error("func:validateCode %n _role:" + _role + "%n" + e.getMessage());
            result.put("code", 500);
            result.put("message", e.getMessage());
            return result;
        }
    }

    /**
     * 修改角色信息和权限
     *
     * @param _role
     * @return
     */
    @RequestMapping(value = "/updateRoleInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateRoleList(@RequestBody Role _role) {
        Map<String, Object> results = new HashMap<String, Object>();

        if (_role == null) {
            results.put("code", 400);
            results.put("message", "操作失败！请联系管理员！");
            results.put("data", "-1");
            return results;
        }

        try {
            Integer resultcount_R = this.roleService.updateRoleAll(_role);
            if (resultcount_R > 0) {
                results.put("code", 200);
                results.put("message", "操作成功！");
                results.put("data", "");
            } else {
                results.put("code", 201);
                results.put("message", "操作失败！请联系管理员！");
                results.put("data", "");
            }
            return results;

        } catch (Exception e) {
            log.error("func:updateRoleInfo %n _role:" + _role + "%n" + e.getMessage());
            results.put("code", 500);
            results.put("message", e.getMessage());
            return results;
        }
    }

    /**
     * 删角色信息
     *
     * @param _role
     * @return
     */
    @RequestMapping(value = "/delRoleInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> delRoleList(@RequestBody Role _role) {
        Map<String, Object> results = new HashMap<String, Object>();

        if (_role == null) {
            results.put("code", 400);
            results.put("message", "操作失败！请联系管理员！");
            results.put("data", "-1");
            return results;
        }

        try {
            Integer resultcount_R = this.roleService.delRoleAll(_role);

            if (resultcount_R >= 0) {
                results.put("code", 200);
                results.put("message", "操作成功！");
                results.put("data", _role);
            } else {
                results.put("code", 201);
                results.put("message", "操作失败！请联系管理员！");
                results.put("data", "-1");
            }
            return results;

        } catch (Exception e) {
            log.error("func:delRoleInfo %n _role:" + _role + "%n" + e.getMessage());
            results.put("code", 500);
            results.put("message", e.getMessage());
            return results;
        }
    }

    /**
     * 获取角色权限信息
     * 原写:caojianhua  修改:liuboqing  3/12
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/getPowerModel", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getPowerModel(@RequestBody Menu _menu) {
        //@RequestBody String  parentId) {

        Map<String, Object> result = new HashMap<String, Object>();
        try {
            List<Menu> p = this.roleService.getPowerModel(_menu.getId(), _menu.getParentId());

            result.put("code", 200);
            result.put("message", "succ");
            result.put("data", p);
            return result;

        } catch (Exception e) {
            log.error("func:getPowerModel:" + e.getMessage());
            result.put("code", 500);
            result.put("message", e);
            return result;
        }

    }

}
